"""
用户权限方面模块，包括用户登录、用户注册
"""
from flask import Blueprint, render_template, request, redirect, url_for, session
from exts import db
from .forms import RegisterForm, LoginForm
from models import UserModel, studentModel,teacherModel,workerModel

# 这里定义的权限视图函数url前缀为"/auth",例如登录可以为/auth/login
bp = Blueprint("auth", __name__, url_prefix="/auth")

# url:"http://127.0.0.1:5000/auth/login"
@bp.route("/login", methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template("login.html")  # 注意这里的前端html文件命名
    else:
        # 从前端获取数据验证用户名和密码是否匹配
        userName = request.form.get("userName")
        passWord = request.form.get("passWord")
        userType = request.form.get("userType")
        user = UserModel.query.filter_by(userName=userName, passWord=passWord, userType=userType).first()
        if not user:
            print("请检查用户名和密码是否输入正确以及用户类别是否勾选正确")
            return redirect(url_for("auth.login"))
        else:
            print("用户名和密码匹配")
            session['user_id'] = user.id
        if userType == "worker":
            return redirect(url_for("backend.backEndIndex"))
        elif userType=='teacher':
            return redirect(url_for('tea.teaIndex'))
        return redirect(url_for("stu.stuIndex"))


@bp.route("/register", methods=['GET', 'POST'])
def register():
    if request.method == 'GET':
        return render_template("register.html")  # 注意这里的前端html文件命名
    else:
        # 从前端获取数据验证格式是否正确
        form = RegisterForm(request.form)
        if form.validate():
            # todo:将数据提交到数据库中
            userName = form.userName.data
            passWord = form.passWord.data
            email = form.email.data
            trueName = request.form.get("trueName")
            phone = form.phone.data
            userType = request.form.get("userType")
            if userType == "student":
                school = request.form.get("school")
                schoolId = request.form.get("schoolId")
                yard = request.form.get("yard")
                major = request.form.get("major")
                inSchoolTime = request.form.get("inschoolTime")
                student = studentModel(userName=userName, passWord=passWord, email=email, trueName=trueName
                                       , phone=phone, userType=userType, school=school, schoolId=schoolId, yard=yard
                                       , major=major, inSchoolTime=inSchoolTime)
                db.session.add(student)
                db.session.commit()
            elif userType == "teacher":
                tSchool = request.form.get("tSchool")
                teachId = request.form.get("teachId")
                teacher = teacherModel(userName=userName, passWord=passWord, email=email, trueName=trueName
                                       , phone=phone, userType=userType,tSchool=tSchool,teachId=teachId)
                db.session.add(teacher)
                db.session.commit()
            elif userType =="worker":
                workId = request.form.get("workId")
                worker = workerModel(userName=userName, passWord=passWord, email=email, trueName=trueName
                                       , phone=phone, userType=userType,workId=workId)
                db.session.add(worker)
                db.session.commit()
            return redirect(url_for("auth.login"))  # 注册成功后跳转到登录页面
        else:
            print(form.errors)
            return redirect(url_for("auth.register"))
